package com.sjzs.web.mail;
import java.io.File;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
 * @description: 创建内含附件、图文并茂的邮件
 */
public class WithAttachmentMessage {
	/**
	 * @param fileName 文件名
	 * @return  MimeBodyPart
	 * @throws Exception
	 * @description: 根据传入的文件路径创建附件并返回
	 */
	public MimeBodyPart createAttachment(String fileName) throws Exception {
		MimeBodyPart attachmentPart = new MimeBodyPart();
		File file = new File(fileName);
		if (file.exists()) {
			FileDataSource fds = new FileDataSource(fileName);
			attachmentPart.setDataHandler(new DataHandler(fds));
			attachmentPart.setFileName(fds.getName());
		}
		return attachmentPart;
	}
	/**
	 * @param body 文本内容
	 * @param fileName 文件名
	 * @return MimeBodyPart
	 * @throws Exception
	 * @description: 根据传入的邮件正文body和文件路径创建图文并茂的正文部分
	 */
	public MimeBodyPart createContent(String body, String fileName) throws Exception {
		// 用于保存最终正文部分
		MimeBodyPart contentBody = new MimeBodyPart();
		// 用于组合文本和图片，"related"型的MimeMultipart对象
		MimeMultipart contentMulti = new MimeMultipart("related");

		// 正文的文本部分
		MimeBodyPart textBody = new MimeBodyPart();
		textBody.setContent(body, "text/html;charset=gbk");
		contentMulti.addBodyPart(textBody);

		// 正文的图片部分
		MimeBodyPart jpgBody = new MimeBodyPart();
		File file = new File(fileName);
		if (file.exists()) {
			FileDataSource fds = new FileDataSource(fileName);
			jpgBody.setDataHandler(new DataHandler(fds));
			jpgBody.setContentID("logo_jpg");
		}
		contentMulti.addBodyPart(jpgBody);

		// 将上面"related"型的 MimeMultipart 对象作为邮件的正文
		contentBody.setContent(contentMulti);
		return contentBody;
	}
	/**
	 * @param session 会话
	 * @return MimeMessage
	 * @throws Exception
	 * @description: 根据传入的 Seesion 对象创建混合型的 MIME消息(测试用例)
	 */
	public MimeMessage createMessage(Session session) throws Exception {

		String from = "373492727@qq.com";
		String to = "497817048@qq.com";

		String subject = "创建内含附件、图文并茂的邮件！";
		String body = "<h4>内含附件、图文并茂的邮件测试！！！</h4> </br>" + "<a href = http://haolloyin.blog.51cto.com/> 蚂蚁</a></br>" + "<img src = \"http://t10.baidu.com/it/u=1876436922,2152955967&fm=59\"></a>";

		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(from));
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		msg.setSubject(subject);

		// 创建邮件的各个 MimeBodyPart 部分
		MimeBodyPart attachment01 = createAttachment("F:\\aaa.txt");
		MimeBodyPart attachment02 = createAttachment("F:\\123.rar");
		MimeBodyPart attachment03 = createAttachment("F:\\123.jpg");
		MimeBodyPart content = createContent(body, "F:\\123.jpg");
		
		MimeBodyPart textBody = new MimeBodyPart();
		textBody.setContent("ssssssssssssssssssssss", "text/html;charset=gbk");

		// 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象
		MimeMultipart allPart = new MimeMultipart("mixed");
		allPart.addBodyPart(attachment01);
		allPart.addBodyPart(attachment02);
		allPart.addBodyPart(attachment03);
		allPart.addBodyPart(content);
		allPart.addBodyPart(textBody);
		// 将上面混合型的 MimeMultipart 对象作为邮件内容并保存
		msg.setContent(allPart);
		msg.saveChanges();
		return msg;
	}


	/**
	 * @param mail 邮件
	 * @param session 会话
	 * @return MimeMessage
	 * @throws Exception
	 * @description: 根据传入的 Seesion 对象创建混合型的 MIME消息
	 */
	public MimeMessage createMessage(MailInfo mail, Session session) throws Exception {
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(mail.getFrom()));
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mail.getTo()));
		msg.setSubject(mail.getSubject());

		// 创建邮件的各个 MimeBodyPart 部分
		MimeBodyPart content = new MimeBodyPart();
		content.setContent(mail.getContent(), "text/html;charset=gbk");

		// 将邮件中各个部分组合到一个"mixed"型的 MimeMultipart 对象
		MimeMultipart allPart = new MimeMultipart("mixed");
		allPart.addBodyPart(content);
		// 将上面混合型的 MimeMultipart 对象作为邮件内容并保存
		msg.setContent(allPart);
		msg.saveChanges();
		return msg;
	}
}